$(function () {

    //新增
    $("[name='btn_save_new_region']").click(function () {
        var form = window.form_new_region;
        var parentId = form.parentId.value;
        var name = form.name.value;
        var regionType = form.regionType.value;
        var region = {parentId: parentId, name: name, regionType: regionType};

        $.postJsonBody("/setRegion", region, function (re) {
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });


    //编辑
    $("[name='btn_save_edit_region']").click(function () {
        var form = window.form_edit_region;
        var id = form.id.value;
        var parentId = form.parentId.value;
        var name = form.name.value;
        var regionType = form.regionType.value;
        var region = {id: id,parentId: parentId, name: name, regionType: regionType};

        $.putJsonBody("/updateRegion" , region, function (re) {

        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    //删除
    $("[name='btn_delete_region']").click(function () {
        var id = $(this).data("region-id");
        bootbox.bconfirm("确定要删除这个地区吗？", function (re) {
            if (re) {
                $.delete("/deleteRegion/" + id, function (re) {
                    if (re.data) {
                        // tipper.success("删除成功，请刷新页面");
                    	window.location.href=window.location.href; 
                    	window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });

    // 详情
    $('#md_edit_region').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("region-id");
        var modal = $(this);
        var form = window.form_edit_region;
        $.get("/gteRegion/" + id, function (re) {
            var region = re.data;
            form.id.value = region.id;
            form.parentId.value = region.parentId;
            form.name.value = region.name;
            form.regionType.value = region.regionType;

        });
    });
})